import { getToken, setToken, removeToken } from '@/utils/token'
import { login } from '@/api/user'
export default {
  namespaced: true,
  state: {
    token: getToken(),
    userInfo: {}
  },
  mutations: {
    // 保存token到Vuex
    setToken (state, token) {
      state.token = token
      setToken(token)
    },
    // 保存用户基本信息
    setUserInfo (state, user) {
      state.userInfo = user
    },
    // 退出登录移出tokne
    delToken (state) {
      state.token = ''
      removeToken()
    }
  },
  actions: {
    // 登录
    async login (context, loginForm) {
      const res = await login(loginForm)
      context.commit('setToken', res.data.jwt)
    }

  }
}
